import 'package:dio/src/response.dart';
import 'package:flutter/material.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/common/widget/avartar_view.dart';
import 'package:p_location/common/widget/norm_item.dart';
import 'package:p_location/home/constant/home_http_url.dart';
import 'package:p_location/home/obj/employee_info_obj.dart';
import 'package:p_location/home/res/employee_detail_res.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_listener.dart';

class PageEmployeeDetail extends StatefulWidget {
  final int id;
  final String name;

  PageEmployeeDetail(this.id, this.name);

  @override
  _PageEmployeeDetailState createState() => _PageEmployeeDetailState();
}

class _PageEmployeeDetailState extends State<PageEmployeeDetail>
    implements NetListener {
  EmployeeInfoObj _list;
  bool _isLoading = true;
  String errorMsg;

  @override
  void initState() {
    super.initState();
    _requestDetail();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              onPressed: () {
                Navigator.pop(context);
                // 不写onPressed 默认，这个图片不能点击 且会有不可点击的样式（和 写了这个有不同的样式）
                // 如果有 onPressed 但是值是null 也会是不可点击的样式
              }),
          automaticallyImplyLeading: true,
          title: Text(widget.name),
          centerTitle: true,
          elevation: 0,
          brightness: Brightness.dark,
        ),
        body: _isLoading
            ? Center(
                child: CircularProgressIndicator(),
              )
            : Container(
                child: ListView(
                  children: [
                    Container(
                      width: 100,
                      height: 100,
                      alignment: AlignmentDirectional.center,
                      margin: EdgeInsets.only(right: 10),
                      child: AvatarView(
                          '${DioHttpClient.baseUrl}${_list?.image?.downloadUrl ?? ""}'),
                    ),
                    NormItem("姓名", _list.realName),
                    NormItem("定位卡号", _list.sn),
                    NormItem("曾用名", _list.oldName),
                    NormItem("身份", _list.roleName),
                    NormItem("部门", _list.departName),
                    NormItem("民族", _list.nationName),
                    NormItem("证件号码", _list.idCardNo),
                    NormItem("健康状况", _list.healthStatusName),
                    NormItem("专业", _list.major),
                    NormItem("通讯地址", _list.address),
                    NormItem("工作单位", _list.workCompany),
                    NormItem("岗位", _list.jobName),
                    NormItem("证件类型", _list.idCardName),
                    NormItem("联系电话", _list.telephone),
                    NormItem("学历", _list.educationName),
                    NormItem("户籍所在地", _list.census),
                    NormItem("职称", _list.educationName),
                  ],
                ),
              ));
  }

  _requestDetail() async {
    Map<String, dynamic> params = {};
    params["userId"] = widget.id;
    await DioHttpClient.requestGet(
        context, HomeHttpUrl.EMPLOYEE_DETAIL_URL, params, this);
  }

  @override
  void onRequestSuccess(Response response) {
    if (response != null && response.data != null) {
      EmployeeDetailRes res = EmployeeDetailRes.fromJson(response.data);
      if (res != null && res.data != null) {
        _isLoading = false;
        if (res != null && res.data != null) {
          _list = res.data;
        }
        setState(() {});
      }
    }
  }

  @override
  void onRequestError(CommonRes errorRes) {}
}
